﻿using System;
using System.ServiceProcess;
using Microsoft.Owin.Hosting;
using System.Threading;
using Sustainalytics.Utils.Configuration;
using Sustainalytics.ReportGenerator.BL;

namespace Sustainalytics.PdfGeneration.Service
{
    partial class PdfGenerationService : ServiceBase
    {
        public PdfGenerationService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                var url = HostConfig.GetHostUrl();
                WebApp.Start<Startup>(url);
            }
            catch (Exception ex)
            {
                EventLog.Log = ex.Message;
                Startup.IsGenerationRunning = false;
                Startup.Token.Dispose();
                Startup.Token = new CancellationTokenSource();
                Startup.TtlManager = new TTLManager(SettingsHelper.Read("TimeToLive", 60 * 60 * 1000));
                Startup.TtlManager.Start();
            }
        }

        protected override void OnStop()
        {
        }
    }
}
